/**
 * @version 1.0
 * @author Ha Giang
 * @date 
 */
package Communications;

import Configuration.Utils;
import java.io.*;
import java.net.*;

/**
 * This server doesn't handle threads
 * @deprecated  DONT USE ANTMORE
 * @author Ha Giang
 */
public class UnicastServer {

    ServerSocket serverSocket = null;
    Socket clientSocket;
    PrintWriter out = null;
    BufferedReader in = null;
    int port ;//= Utils.UNI_NOTIFY_PORT;//always listen on this port for UnicastServer

    public UnicastServer(int port) {
        this.port = port;
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            System.err.println("Could not listen on port:" + port);
            //System.exit(1);
        }
        clientSocket = null;
    }
    
    public String getIp() {
        try {
            InetAddress addr = InetAddress.getLocalHost();

            // Get IP Address
            byte[] ipAddr = addr.getAddress();

            // Get hostname
            String hostname = addr.getHostAddress();
            return hostname;
        } catch (UnknownHostException e) {
            return null;
        }      
    }
    
    public boolean startListening() {
        System.out.println("Starting Unicast Server Listening on port " + port + "...");
        try {
            clientSocket = serverSocket.accept();
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = new BufferedReader(
                    new InputStreamReader(clientSocket.getInputStream()));
        } catch (IOException e) {
             System.err.println("Could not listen on port:" + port);  
            e.printStackTrace();
            return false;
        } finally {
            System.out.println("Listening ..Done!");
        }
        return true;
    }

    public void sendMessage(String msg) {
        if (!clientSocket.isClosed()) {
            out.println(msg);
        }
    }

    public String getMessage() {
        String s = null;
        //if (!clientSocket.isClosed()) 
        {
            try {
                s = in.readLine();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return s;
    }

    public void close() {
        try {
            serverSocket.close();
            clientSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] arg) {
        System.out.println("Server");
        UnicastServer us = new UnicastServer(Utils.UNICAST_NOTIFY_PORT);
        us.startListening();
        String inputLine;

        int count = 0;
        while ((inputLine = us.getMessage()) != null) {
            System.out.println("Server: " + inputLine);
            //out.println(inputLine); 
            count++;
            //us.out.println(count);
            us.sendMessage(String.valueOf(count));

            if (inputLine.equals("Bye.")) {
                break;
            }
        }
        us.close();
    }
}